package com.ruoyi.project.mgr.adjust.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.mgr.adjust.domain.MgrAdjustNotice;
import com.ruoyi.project.mgr.adjust.mapper.MgrAdjustNoticeMapper;
import com.ruoyi.project.mgr.adjust.vo.AdjustListVO;
import com.ruoyi.project.mgr.adjust.service.IMgrAdjustApplyService;
import com.ruoyi.project.mgr.adjust.vo.NoticeListVO;
import com.ruoyi.project.mgr.arrange.domain.MgrCourseArrange;
import com.ruoyi.project.mgr.arrange.mapper.MgrCourseArrangeMapper;
import com.ruoyi.project.mgr.course.mapper.MgrCourseMapper;
import com.ruoyi.project.mgr.room.domain.MgrRoom;
import com.ruoyi.project.mgr.room.mapper.MgrRoomMapper;
import com.ruoyi.project.system.dict.mapper.DictDataMapper;
import com.ruoyi.project.system.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mgr.adjust.mapper.MgrAdjustApplyMapper;
import com.ruoyi.project.mgr.adjust.domain.MgrAdjustApply;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 调课申请Service业务层处理
 *
 * @author .
 * @date 2020-02-15
 */
@Service
public class MgrAdjustApplyServiceImpl implements IMgrAdjustApplyService
{
    @Autowired
    private MgrAdjustApplyMapper mgrAdjustApplyMapper;
    @Autowired
    private MgrAdjustNoticeMapper mgrAdjustNoticeMapper;
    @Autowired
    private MgrCourseArrangeMapper mgrCourseArrangeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MgrRoomMapper mgrRoomMapper;
    @Autowired
    private MgrCourseMapper mgrCourseMapper;
    @Autowired
    private DictDataMapper dictDataMapper;

    /**
     * 查询调课申请
     *
     * @param id 调课申请ID
     * @return 调课申请
     */
    @Override
    public MgrAdjustApply selectMgrAdjustApplyById(Long id)
    {
        return mgrAdjustApplyMapper.selectMgrAdjustApplyById(id);
    }

    /**
     * 查询调课申请列表
     *
     * @param mgrAdjustApply 调课申请
     * @return 调课申请
     */
    @Override
    public List<MgrAdjustApply> selectMgrAdjustApplyList(MgrAdjustApply mgrAdjustApply)
    {
        return mgrAdjustApplyMapper.selectMgrAdjustApplyList(mgrAdjustApply);
    }

    /**
     * 新增调课申请
     *
     * @param mgrAdjustApply 调课申请
     * @return 结果
     */
    @Override
    public int insertMgrAdjustApply(MgrAdjustApply mgrAdjustApply)
    {
        mgrAdjustApply.setInitiateTime(new Date());
        MgrCourseArrange courseArrange = mgrCourseArrangeMapper.selectMgrCourseArrangeById(mgrAdjustApply.getArrangeId());
        mgrAdjustApply.setTeacherId(courseArrange.getTeacherId());
        mgrAdjustApply.setTeacherName(userMapper.selectUserById(courseArrange.getTeacherId()).getUserName());
        mgrAdjustApply.setRoomCode(mgrRoomMapper.selectMgrRoomById(courseArrange.getRoomId()).getRoomCode());
        mgrAdjustApply.setCourseDate(courseArrange.getCourseDate());
        mgrAdjustApply.setCourseName(mgrCourseMapper.selectMgrCourseById(courseArrange.getCourseId()).getCourseName());
        mgrAdjustApply.setTimeSlot(courseArrange.getTimeSlot());

        return mgrAdjustApplyMapper.insertMgrAdjustApply(mgrAdjustApply);
    }

    /**
     * 修改调课申请
     *
     * @param mgrAdjustApply 调课申请
     * @return 结果
     */
    @Override
    public int updateMgrAdjustApply(MgrAdjustApply mgrAdjustApply)
    {
        return mgrAdjustApplyMapper.updateMgrAdjustApply(mgrAdjustApply);
    }

    /**
     * 删除调课申请对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMgrAdjustApplyByIds(String ids)
    {
        return mgrAdjustApplyMapper.deleteMgrAdjustApplyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除调课申请信息
     *
     * @param id 调课申请ID
     * @return 结果
     */
    @Override
    public int deleteMgrAdjustApplyById(Long id)
    {
        return mgrAdjustApplyMapper.deleteMgrAdjustApplyById(id);
    }

    @Override
    public List<AdjustListVO> selectAdjustApplyList(MgrAdjustApply mgrAdjustApply) {
        return mgrAdjustApplyMapper.selectAdjustApplyList(mgrAdjustApply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int agree(MgrAdjustApply adjustApply) {
        // 处理状态更新为已处理
        adjustApply.setOperateStatus(1);
        // 处理结果更新为同意
        adjustApply.setOperateResult(0);
        // 更新申请表数据
        mgrAdjustApplyMapper.updateMgrAdjustApply(adjustApply);
        // 申请方课程
        MgrCourseArrange originalArrange = mgrCourseArrangeMapper.selectMgrCourseArrangeById(adjustApply.getArrangeId());
        if ("0".equals(adjustApply.getType())) {

            // 申请方教师
            Long teacherId = originalArrange.getTeacherId();
            // 课程
            Long courseId = originalArrange.getCourseId();

            // 调整方课程
            MgrCourseArrange nowArrange = mgrCourseArrangeMapper.selectMgrCourseArrangeById(adjustApply.getAfterArrangeId());

            Long nowTeacherId = nowArrange.getTeacherId();
            // 课程
            Long nowCourseId = nowArrange.getCourseId();
            // 调换两个排课的教师和课程
            originalArrange.setTeacherId(nowTeacherId);
            originalArrange.setCourseId(nowCourseId);
            mgrCourseArrangeMapper.updateMgrCourseArrange(originalArrange);
            nowArrange.setTeacherId(teacherId);
            nowArrange.setCourseId(courseId);
            mgrCourseArrangeMapper.updateMgrCourseArrange(nowArrange);

            // 添加申请方通知消息
            MgrAdjustNotice adjustNotice = new MgrAdjustNotice();
            adjustNotice.setApplyId(adjustApply.getId());
            adjustNotice.setTeacherId(teacherId);
            adjustNotice.setClazzId(originalArrange.getClazzId());
            adjustNotice.setAdjustTime(new Date());
            mgrAdjustNoticeMapper.insertMgrAdjustNotice(adjustNotice);
            // 添加调整方通知消息
            MgrAdjustNotice notice = new MgrAdjustNotice();
            notice.setApplyId(adjustApply.getId());
            notice.setTeacherId(nowTeacherId);
            notice.setClazzId(nowArrange.getClazzId());
            notice.setAdjustTime(new Date());
            notice.setApplyType("1");
            notice.setType(adjustApply.getType());
            mgrAdjustNoticeMapper.insertMgrAdjustNotice(notice);
        } else if ("1".equals(adjustApply.getType())) {
            originalArrange.setCourseDate(adjustApply.getCourseDate());
            MgrRoom mgrRoom = new MgrRoom();
            mgrRoom.setRoomCode(adjustApply.getNowRommCode());
            originalArrange.setRoomId(mgrRoomMapper.selectMgrRoomList(mgrRoom).get(0).getRoomId());
            originalArrange.setTimeSlot(adjustApply.getNowTimeSlot());
            mgrCourseArrangeMapper.updateMgrCourseArrange(originalArrange);

            MgrAdjustNotice adjustNotice = new MgrAdjustNotice();
            adjustNotice.setApplyId(adjustApply.getId());
            adjustNotice.setTeacherId(originalArrange.getTeacherId());
            adjustNotice.setClazzId(originalArrange.getClazzId());
            adjustNotice.setAdjustTime(new Date());
            adjustNotice.setType(adjustApply.getType());
            mgrAdjustNoticeMapper.insertMgrAdjustNotice(adjustNotice);
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int refuse(MgrAdjustApply mgrAdjustApply) {
        MgrAdjustApply adjustApply = mgrAdjustApplyMapper.selectMgrAdjustApplyById(mgrAdjustApply.getId());
        // 处理状态更新为已处理
        adjustApply.setOperateStatus(1);
        // 处理结果更新为拒绝
        adjustApply.setOperateResult(1);
        adjustApply.setReason(mgrAdjustApply.getReason());
        mgrAdjustApplyMapper.updateMgrAdjustApply(adjustApply);

        // 申请方课程
        MgrCourseArrange originalArrange = mgrCourseArrangeMapper.selectMgrCourseArrangeById(adjustApply.getArrangeId());
        MgrAdjustNotice notice = new MgrAdjustNotice();
        notice.setApplyId(mgrAdjustApply.getId());
        notice.setTeacherId(originalArrange.getTeacherId());
        notice.setClazzId(originalArrange.getClazzId());
        notice.setAdjustTime(new Date());
        notice.setType(mgrAdjustApply.getType());
        return mgrAdjustNoticeMapper.insertMgrAdjustNotice(notice);
    }

    @Override
    public List<NoticeListVO> selectStudentNotice(Long clazzId) {
        return mgrAdjustNoticeMapper.selectStudentNotice(clazzId);
    }

    @Override
    public List<NoticeListVO> selectTeacherNotice(Long teacherId) {
        return mgrAdjustNoticeMapper.selectTeacherNotice(teacherId);
    }
}